#pragma once
#include"log.hpp"
#include<string>
#include"Init.hpp"
extern Log lg;
Init init;
class Task//获取客户端的信息，处理信息并返回
{
    private:
    int sock_fd;
    uint16_t clientport_;
    std::string clientip_;
    public:
    Task(const int& fd,const uint16_t& port,const std::string &ip)
    :sock_fd(fd),clientport_(port),clientip_(ip)
    {

    }
    void operator()()
    {
        Run();
    }
    void Run()
    {
        char buffer[1024];
       ssize_t n= read(sock_fd,buffer,sizeof(buffer));
       if(n<0)
       {
        lg(FATAL,"read err！！errno is:%d, errsring is: %s",errno,strerror(errno));
       }
       else if(n==0)
       {
        lg(FATAL,"Client close！！clientip :%s, clientport: %d, sorkfd: %d",clientip_.c_str(),clientport_,sock_fd);
       }
       else{
        buffer[n]='\0';
        std::string key=buffer;
        std::string echo_message=init.translation(key);
        write(sock_fd,echo_message.c_str(),echo_message.size());
       }
       close(sock_fd);
    }


};